// components/loginpop/loginpop.ts
import api from "../../utils/api"

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    visible: false,
    login: true as boolean,//登录弹窗
    codeLogin: false as boolean,//验证码登录弹窗
    dialogClass: '' as string,//动画类名
    codeDialogClass: '' as string,//动画类名
    shakeClass: '' as string,//抖动类名
    agree: false as boolean,//是否同意协议
    imageCode: false as boolean,//是否展示图形验证码
    time: 60 as number,//倒计时
    isSend: false as boolean,//是否已经发送验证码
    phone: '' as string,//手机号
    smsCode: '' as string,//手机验证码
    successCallback: null as any,
    failCallback: null as any
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 验证码登录
    toCodeLogin(): void {
      this.setData({
        dialogClass: 'slide-out',
        codeDialogClass: 'slide-in',
        codeLogin: true
      })
      setTimeout(() => {
        this.setData({
          login: false
        })
      }, 500)

    },
    // 点击同意协议
    changeStatus(): void {
      this.setData({
        agree: !this.data.agree
      })
    },
    // 手机号直接登录
    async getPhoneNumber(e: any): Promise<void> {
      const params = {
        code: e.detail.code,
        channel: getApp().globalData.channel
      }
      api.login.wxPhoneLogin(params).then(res => {
        if (res.data.code == '000') {
          this.hide();
          if (this.data.successCallback) {
            //@ts-ignore
            this.data.successCallback();
          } else {
            wx.switchTab({
              url: "/pages/index/index"
            })
          }
        } else {
          wx.showToast({
            title: res.data.message as string,
            icon: 'error',
            duration: 2000
          })
          console.error(res.data.message)
        }
      })
    },
    // 未勾选的提示
    toTip(): void {
      this.setData({ shakeClass: 'shake' });
      setTimeout(() => this.setData({ shakeClass: '' }), 500);
    },
    // 点击获取手机验证码，弹窗展示图形验证码
    showImageCode(): void {
      let mobilePattern = /^1[3-9]\d{9}$/;
      if (this.data.isSend) {
        return;
      }
      if (mobilePattern.test(this.data.phone)) {
        this.setData({
          imageCode: true
        })
      } else {
        wx.showToast({
          title: "手机号格式错误",
          icon: "error"
        })
      }
    },
    // 关闭验证码弹窗
    closeImageCode(): void {
      this.setData({
        imageCode: false
      })
    },
    // 暂不登录
    exit(): void {
      // wx.exitMiniProgram();
      this.hide();
      if (this.data.failCallback) {
        //@ts-ignore
        this.data.failCallback();
      }
    },
    // 倒计时
    countDown(): void {
      let countdown = this.data.time;
      const timer = setInterval(() => {
        countdown--;
        this.setData({
          time: countdown
        });

        if (countdown === 0) {
          clearInterval(timer);
          this.setData({
            isSend: false,
            time: 60
          });
        }
      }, 1000);
    },
    // 手机验证码登录
    async codeLogin(): Promise<void> {
      if (!this.data.agree) {
        this.setData({ shakeClass: 'shake' });
        setTimeout(() => this.setData({ shakeClass: '' }), 500);
        return
      }
      const { phone, smsCode } = this.data
      if (!phone || !smsCode) {
        wx.showToast({
          title: "请检查输入",
          icon: "error"
        })
        return
      }
      const params = {
        phone,
        smsCode,
        channel: getApp().globalData.channel
      }
      api.login.smsLogin(params).then(res => {
        if (res.data.code == "000") {
          this.hide();
          if (this.data.successCallback) {
            //@ts-ignore
            this.data.successCallback();
          } else {
            this.hide();
            wx.switchTab({
              url: "/pages/index/index"
            })
          }
        } else {
          wx.showToast({
            title: res.data.message as string,
            icon: "error"
          })
        }
      })
    },
    // 验证code
    checkImageCode(e: any): void {
      const { code, validateImageKey } = e.detail
      const params: LoginSendSmsCodeParam = {
        phone: this.data.phone,
        validateImageKey,
        validateImageValue: code
      }
      api.login.sendSmsCode(params).then(res => {
        if (res.data.code == "000") {
          this.setData({
            imageCode: false
          })
          this.countDown();
          this.setData({
            isSend: true
          })
        } else {
          wx.showToast({
            title: res.data.message as string,
            icon: "error"
          })
        }
      })
    },
    // 输入手机号
    handlePhone(e: any): void {
      this.setData({
        phone: e.detail.value
      })
    },
    // 输入手机验证码
    handleCode(e: any): void {
      this.setData({
        smsCode: e.detail.value
      })
    },
    show(success: any, fail: any) {
      if (getApp().globalData.loginShowing) {
        return;
      }
      getApp().globalData.loginShowing = true;
      this.data.successCallback = success;
      this.data.failCallback = fail
      this.setData({
        agree: false,
        visible: true
      })
    },
    hide() {
      if (!getApp().globalData.loginShowing) {
        return;
      }
      getApp().globalData.loginShowing = false;
      this.setData({
        visible: false
      })
    },
    toDirLogin(): void {
      this.setData({
        dialogClass: 'slide-dialog',
        codeDialogClass: 'slide-code-dialog',
        login: true
      })
      setTimeout(() => {
        this.setData({
          codeLogin: false
        })
      }, 500)
    },
    toUser(): void {
      wx.navigateTo({
        url:"/pages/about/about?title=1"
      })
    },
    toPersonal(): void {
      wx.navigateTo({
        url:"/pages/about/about?title=2"
      })
    },
    toNoDuty(): void {
      wx.navigateTo({
        url:"/pages/about/about?title=3"
      })
    }
  }
})